package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.constants.CourseConstants;
import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.domain.CourseDetail;
import cn.itsource.hrm.domain.CourseMarket;
import cn.itsource.hrm.dto.CourseSaveDTO;
import cn.itsource.hrm.feignclients.ESFeignClient;
import cn.itsource.hrm.mapper.CourseDetailMapper;
import cn.itsource.hrm.mapper.CourseMapper;
import cn.itsource.hrm.mapper.CourseMarketMapper;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.LoginInfo;
import cn.itsource.hrm.util.PageList;
import cn.itsource.hrm.util.UserContext;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luobiao
 * @since 2020-02-21
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseDetailMapper courseDetailMapper;

    @Autowired
    private CourseMarketMapper courseMarketMapper;
    
    @Autowired
    private ESFeignClient esFeignClient;

    @Override
    public void saveOrUpdate(CourseSaveDTO courseSaveDTO) {
        //添加课程----------------------
        //status
        Date date = new Date();
        Course course = courseSaveDTO.getCourse();
        course.setStatus(CourseConstants.STATUS_OFFLINE);

        //开始截止时间假数据模拟
        course.setStartTime(date);
        course.setEndTime(date);
        LoginInfo loginInfo = UserContext.getLoginInfo();
        //机构id
        course.setTenantId(loginInfo.getTenantId());
        //机构名字
        course.setTenantName(loginInfo.getTenantName());
        //添加课程的用户id
        course.setUserId(loginInfo.getUserId());
        //添加课程的用户名
        course.setUserName(loginInfo.getUserName());
        //添加课程
        baseMapper.insert(course);

        //添加课程详情---------------------
        CourseDetail courseDetail = courseSaveDTO.getCourseDetail();
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);

        //添加课程营销-----------------------
        CourseMarket courseMarket = courseSaveDTO.getCourseMarket();
        courseMarket.setId(course.getId());
        //假数据，原价现价设为一样
        courseMarket.setPriceOld(courseMarket.getPrice());
        //截止时间:假数据
        courseMarket.setExpires(date);
        //添加课程营销
        courseMarketMapper.insert(courseMarket);
    }

    //课程上线
    @Override
    public void onLineCourse(Long id) {
        //根据id查询状态
        Course course = baseMapper.selectById(id);
        //判断课程状态是否为
        if(course == null || course.getStatus()!=CourseConstants.STATUS_OFFLINE){
            throw new RuntimeException("你的课程不是处于下线状态或者课程为空");
        }
        // 添加DOc数据到es
        //1.需要在es-server中写controller接口
        //2.课程通过feign调用es的controller
        CourseDoc courseDoc = new CourseDoc();
        //封装CourseDoc，需要course，market，的信息
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        CourseDetail courseDetail = courseDetailMapper.selectById(id);
        BeanUtils.copyProperties(course,courseDoc);
        BeanUtils.copyProperties(courseMarket,courseDoc);
        BeanUtils.copyProperties(courseDetail,courseDoc);
        courseDoc.setChargeName(courseMarket.getCharge()==1?"免费":"收费");
        //设置浏览数
        courseDoc.setViewCount(10000);
        //设置购买数
        courseDoc.setBuyCount(5000);

        AjaxResult ajaxResult = esFeignClient.save(courseDoc);

        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("课程上线失败");
        }
        //如果添加es成功
        //将数据库中状态改为上线
        course.setStatus(CourseConstants.STATUS_ONLINE);
        baseMapper.updateById(course);
    }

    /*
    * 课程下线
    * */
    @Override
    public void offLineCourse(Long id) {
        //根据id查询状态
        Course course = baseMapper.selectById(id);
        //判断课程状态是否为
        if(course == null || course.getStatus()!=CourseConstants.STATUS_ONLINE){
            throw new RuntimeException("你的课程不是处于上线状态或者课程为空");
        }
        // 删除es中的数据
        //1.需要在es-server中写controller接口
        //2.课程通过feign调用es的controller

        AjaxResult ajaxResult = esFeignClient.deleteById(id);
        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("课程下线失败");
        }
        //如果删除es成功
        //将数据库中状态改为下线
        course.setStatus(CourseConstants.STATUS_OFFLINE);
        baseMapper.updateById(course);
    }

    /*
    * 查询课程列表
    * */
    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        return esFeignClient.searchCourse(courseQuery);
    }
}
